package com.cjst.service.intelligent.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cjst.service.dubbo.BaiduMindDubboService;
import com.cjst.service.dubbo.BroadCastDubboService;
import com.cjst.service.dubbo.TengxunDubboService;
import com.cjst.service.intelligent.IntelligentControlService;
import com.cjst.util.FileToByte;

/**
 * 智能控制
 * @author yj
 *
 */
@Service
public class IntelligentControlServiceImpl implements IntelligentControlService{
	@Reference
	private BaiduMindDubboService baiduMindDubboService;
	@Reference
	private BroadCastDubboService broadCastDubboService;
	@Reference
	private TengxunDubboService tengxunDubboService;
	@Value("${img.location}")
	private String uploadDir;
	@Value("${file.path}")
	private String url;
	@Override
	public Map<String, Object> imageControl(String fileDir,int type){
		Map<String, Object> map=new HashMap<String, Object>();
		System.out.println("1111111111111111111111111111111111111111:"+fileDir);
		String imageResult=baiduMindDubboService.getBaiduImage(null, FileToByte.readFile(fileDir), type, 0);
		JSONObject jsonObject = JSONObject.parseObject(imageResult);
		String msg=jsonObject.getString("msg");
		Integer resultCode=jsonObject.getInteger("resultCode");
		map.put("code", resultCode);
		//map.put("msg", msg);
		System.out.println("=============resultCode:"+resultCode);
		if(resultCode.equals(200)){
			String strData=jsonObject.getString("data");
			JSONArray data=JSONArray.parseArray(strData);
			if(data!=null){
				for (int i = 0; i < data.size(); i++) {
					JSONObject r=data.getJSONObject(i);
					int score=r.getInteger("score");
					String name=r.getString("name");
					String imageUrl=r.getString("imageUrl");
					String description=r.getString("description");
					//broadCastDubboService.skPlay(1, 1, description, "1", 1);
					map.put("score", score);
					map.put("name", name);
					map.put("imageUrl", imageUrl);
					map.put("description", description);
					fileDir=fileDir.replace(uploadDir, "");
					fileDir=fileDir.replace("ys_", "");
					map.put("fileDir", url+fileDir);
					return map;
				}
			}
		}
		return map;
	}
	@Override
	public Map<String, Object> deedImg(byte[] img){
		Map<String, Object> map=new HashMap<String, Object>();
		String needImg=tengxunDubboService.deed(img);
		JSONObject jsonObject = JSONObject.parseObject(needImg);
		String msg=jsonObject.getString("msg");
		Integer resultCode=jsonObject.getInteger("resultCode");
		map.put("code", resultCode);
		//map.put("msg", msg);
		if(resultCode.equals(200)){
			String strData=jsonObject.getString("data");
			JSONObject jsObj=JSONObject.parseObject(strData);
			Float score=jsObj.getFloat("score");
			String type=jsObj.getString("type");
			map.put("score", score);
			map.put("type", type);
		}
		return map;
	}
}










